"""
Case Type   : 全链路跟踪
Case Name   : 支持全链路跟踪，enable_record_nettime参数设置验证
Create At   : 2024/10
Owner       : haomeng
Description :
    1、查看参数默认值
    2、gs_guc set方式设置参数值为on
    3、gs_guc reload方式加载参数值为off
    4、alter database方式设置参数值为true/1
    5、alter user方式设置参数值为false/0
    6、set to 方式设置参数值为yes/no
    7、设置参数为其他异常值
    8、重启数据库，使参数生效
    9、恢复参数默认值
Expect      :
    1、参数默认值为off
    2、设置成功
    3、设置成功
    4、设置成功
    5、设置成功
    6、设置成功
    7、合理报错
    8、重启成功，参数值修改成
    9、恢复参数默认值成功
History     :
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class JDBCQuikAutoBalance(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.comsh = CommonSH('PrimaryDbUser')
        self.pri_user = Node('PrimaryDbUser')
        self.com = Common('PrimaryDbUser')
        self.constant = Constant()
        self.param = 'enable_record_nettime'
        self.user = 'test_record_user'

        self.log.info('-----创建测试用户-----')
        res = self.comsh.execut_db_sql(f"create user {self.user} with sysadmin password '{macro.COMMON_PASSWD}';")
        self.log.info(res)

    def test_record_nettime(self):
        text = '-----step1:查看enable_record_nettime参数默认值;  expect:默认值为off-----'
        self.log.info(text)

        value = self.comsh.execut_db_sql(f'show {self.param}', dbname='postgres')
        self.param_default = value.splitlines()[-2].strip()
        self.log.info(self.param_default)
        self.assertEqual('off', self.param_default, '参数默认值校验失败')

        text = '-----step2:gs_guc set方式设置参数值为on  expect:设置成功-----'
        self.log.info(text)

        set_param1 = self.comsh.execute_gsguc('set',
                                              self.constant.GSGUC_SUCCESS_MSG,
                                              f'{self.param} = on')
        self.log.info(set_param1)
        self.assertTrue(set_param1, 'set方式设置参数失败')

        text = '-----step3:gs_guc reload方式加载参数值为off  expect:设置成功-----'
        self.log.info(text)

        set_param2 = self.comsh.execute_gsguc('reload',
                                              self.constant.GSGUC_SUCCESS_MSG,
                                              f'{self.param} = off')
        self.log.info(set_param2)
        self.assertTrue(set_param2, 'reload方式设置参数失败')

        text = '-----step4:alter database方式设置参数值为true/1  expect:设置成功-----'
        self.log.info(text)

        sql1 = f'alter database {self.pri_user.db_name} set {self.param} to true;'
        self.log.info(sql1)
        sql2 = f'alter database {self.pri_user.db_name} set {self.param} to 1;'
        self.log.info(sql2)

        set_param3 = self.comsh.execut_db_sql(sql1)
        self.log.info(set_param3)
        self.assertIn(self.constant.ALTER_DATABASE_SUCCESS_MSG, set_param3, '参数设置失败')

        set_param4 = self.comsh.execut_db_sql(sql2)
        self.log.info(set_param4)
        self.assertIn(self.constant.ALTER_DATABASE_SUCCESS_MSG, set_param4, '参数设置失败')

        text = '-----step5:alter user方式设置参数值为false/0  expect:设置成功-----'
        self.log.info(text)

        sql3 = f'alter user {self.user} set {self.param} to false;'
        self.log.info(sql3)
        sql4 = f'alter user {self.user} set {self.param} to 0;'
        self.log.info(sql4)

        set_param5 = self.comsh.execut_db_sql(sql3)
        self.log.info(set_param5)
        self.assertIn(self.constant.ALTER_ROLE_SUCCESS_MSG, set_param5, '参数设置失败')

        set_param6 = self.comsh.execut_db_sql(sql4)
        self.log.info(set_param6)
        self.assertIn(self.constant.ALTER_ROLE_SUCCESS_MSG, set_param6, '参数设置失败')

        text = '-----step6:set to方式设置参数值为yes/no  expect:设置成功-----'
        self.log.info(text)

        sql5 = f'set {self.param} to no;'
        self.log.info(sql5)
        sql6 = f'set {self.param} to yes;'
        self.log.info(sql6)

        set_param7 = self.comsh.execut_db_sql(sql5)
        self.log.info(set_param7)
        self.assertIn(self.constant.SET_SUCCESS_MSG, set_param7, '参数设置失败')

        set_param8 = self.comsh.execut_db_sql(sql6)
        self.log.info(set_param8)
        self.assertIn(self.constant.SET_SUCCESS_MSG, set_param8, '参数设置失败')

        text = '-----step7:设置参数为其他异常值  expect:设置失败-----'
        self.log.info(text)

        sql7 = f'set {self.param} to zhende;'
        self.log.info(sql7)
        sql8 = f'set {self.param} to 666;'
        self.log.info(sql8)

        set_param9 = self.comsh.execut_db_sql(sql7)
        self.log.info(set_param9)
        self.assertIn(f'ERROR:  parameter "{self.param}" '
                      f'requires a Boolean value', set_param9, '参数设置成功')

        set_param10 = self.comsh.execut_db_sql(sql8)
        self.log.info(set_param10)
        self.assertIn(f'ERROR:  parameter "{self.param}" '
                      f'requires a Boolean value', set_param10, '参数设置成功')

        text = '-----step8:重启数据库，使参数生效  expect:重启成功，参数修改成功-----'
        self.log.info(text)

        restart = self.comsh.restart_db_cluster()
        self.log.info(restart)
        self.assertTrue(restart)

        param_value = self.com.show_param(self.param)
        self.log.info(param_value)
        self.assertEqual('on', param_value, '参数修改失败')

    def tearDown(self):
        text = '-----step9:恢复参数默认值  expect:恢复成功-----'
        self.log.info(text)

        reset_param = self.comsh.execute_gsguc('set',
                                               self.constant.GSGUC_SUCCESS_MSG,
                                               f'{self.param} = {self.param_default}')
        self.log.info(reset_param)

        restart = self.comsh.restart_db_cluster()
        self.log.info(restart)

        drop_user = self.comsh.execut_db_sql(f'drop user {self.user};')
        self.log.info(drop_user)

        reset_param_value = self.comsh.execut_db_sql(f'show {self.param}', dbname='postgres')
        self.log.info(reset_param_value)

        self.assertEqual('off', reset_param_value.splitlines()[-2].strip(), '恢复默认值失败')
        self.assertIn(self.constant.DROP_ROLE_SUCCESS_MSG, drop_user, '删除用户失败')
        self.assertTrue(restart)
        self.log.info(f'----{os.path.basename(__file__)} end----')
